<template>
  <el-button v-if="detailIsShow != '1' && isShow" :disabled="detailIsShow === '1'" plain type="success"
             @click="submitDetailForm">
    <svg-icon class="mr5" icon-class="save"/>
    保存
  </el-button>
  <el-button v-if="detailIsShow != '1' && !isShow" :disabled="detailIsShow === '1'" plain type="success"
             @click="templateQuote">
    <svg-icon class="mr5" icon-class="save"/>
    模板引用
  </el-button>
  <template v-if="detailIsShow === '1'">
    <el-empty description="暂无数据，请点击上方列表，加载展示数据"/>
  </template>
  <template v-else-if="detailIsShow === 'A'">
    <western-medicine ref="prescriptionItemRef" :apply-is-disabled="true" :org-id="queryParams.orgId"
                      @item-submit-ok="itemSubmitOk" @quote-ok="quoteOk"/>
  </template>
  <template v-else-if="detailIsShow === 'B'">
    <chinese-medicine ref="chineseMedicineRef" :apply-is-disabled="true" :org-id="queryParams.orgId"
                      @item-submit-ok="itemSubmitOk" @quote-ok="quoteOk"/>
  </template>
</template>

<script lang="ts" setup>
import westernMedicine from './westernMedicine.vue'
import chineseMedicine from "./chineseMedicine.vue"
import {addTemplateDetail, listTemplateDetail} from "@/api/patient/templateDetail"

const emits = defineEmits(["quoteOk"])
const prescriptionItemRef = ref()
const chineseMedicineRef = ref()
const props = defineProps({
  queryParams: {
    type: Object as PropType<TemplateMasterType>
  },
  applyItem: {
    type: Object as PropType<TemplateMasterType>
  },
  detailIsShow: {
    type: String,
    default: "1"
  },
  isShow: {
    type: Boolean,
    default: false
  }
})

function submitDetailForm() {
  if (props.detailIsShow === "A") {
    prescriptionItemRef.value.itemValidate()
  } else if (props.detailIsShow === "B") {
    chineseMedicineRef.value.itemValidate()
  }
}


// 子页面保存校验通过后返回事件
function itemSubmitOk(itemList: any) {
  ElMessageBox.confirm('确定要进行保存操作吗？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    saveOk(itemList)
  }).catch(() => {
    ElMessage.info("操作终止")
  })
}

// 最终保存
function saveOk(itemForm: any) {
  let itemList = itemForm.prescriptionItemList
  if (props.applyItem.itemClass === "B") {
    itemList = itemForm.chinesePrescriptionItemList
  } else {
    itemList = itemForm.prescriptionItemList
  }
  itemList.forEach((item: any) => {
    item.templateMasterId = props.applyItem.templateMasterId
    item.itemtype = props.applyItem.type
    item.isApply = "N"
    item.itemClass = props.applyItem.itemClass
    item.itemCode = item.drugCode || item.itemCode
    item.itemName = item.drugName || item.itemName
    item.isDecoction = props.applyItem.itemClass === "B" ? itemForm.isDecoction : item.channel
    item.repetition = props.applyItem.itemClass === "B" ? itemForm.repetition : item.channel
    item.isUrgent = "N"
    item.orgId = props.applyItem.orgId
    item.priceId = item.priceId
    item.channel = props.applyItem.itemClass === "B" ? itemForm.channel : item.channel
    item.performFreq = props.applyItem.itemClass === "B" ? itemForm.performFreq : item.performFreq
  });
  const param = {
    detailBoList: itemList,
    orgId: props.applyItem.orgId,
  }
  addTemplateDetail(param).then(() => {
    rowClick(props.applyItem)
    ElMessage.success("保存成功")
  })
}


// 主表行点击事件
function rowClick(row: any) {
  if (row.itemClass === "A") {
    nextTick(() => {
      prescriptionItemRef.value.templateMene()
      prescriptionItemRef.value.setDrugClassArray(['1', '3'], row.storageId)
      listTemplateDetail({"templateMasterId": row.templateMasterId}).then((res: resType) => {
        row.detailList = res.data
        // if (row.isQuote) {
        //     prescriptionItemRef.value.trDisabledInFalse()
        // }
        prescriptionItemRef.value.applyRowClick(row)
      })
    })
  } else if (row.itemClass === "B") {
    nextTick(() => {
      chineseMedicineRef.value.templateMene()
      chineseMedicineRef.value.setDrugClassArray(['2'], row.storageId)
      listTemplateDetail({"templateMasterId": row.templateMasterId}).then((res: resType) => {
        row.detailList = res.data
        // if (row.isQuote) {
        //     chineseMedicineRef.value.trDisabledInFalse()
        // }
        chineseMedicineRef.value.applyRowClick(row)
      })
    })
  }
}

// 模板引用 -》调用子页面进行勾选数量校验
function templateQuote() {
  if (props.detailIsShow === "A") {
    prescriptionItemRef.value.quoteRules()
  } else if (props.detailIsShow === "B") {
    chineseMedicineRef.value.quoteRules()
  }
}

// 模板引用 -》 最终数据处理
function quoteOk(list: any) {
  if (list.length <= 0) {
    return ElMessage.error("没有勾选药品明细，请勾选后再引用。")
  }
  emits("quoteOk", list)
}

function emptyPrescriptionItemList() {
  if (props.detailIsShow === "A") {
    prescriptionItemRef.value.emptyPrescriptionItemList()
  } else if (props.detailIsShow === "B") {
    chineseMedicineRef.value.emptyPrescriptionItemList()
  }
}

defineExpose({
  rowClick,
  emptyPrescriptionItemList
})
</script>
